<template>
    <!-- register-nav-bar -->
    <div class="Register-nav">
        <van-nav-bar title="注册" left-text="" left-arrow @click-left="onClickLeft" />
    </div>

    <!-- ref验证 -->
    <van-form @submit="submitForm" @failed="onFailed" ref="ruleFormRef" :model="form">

        <van-cell-group>
            <!-- clearable：启用清除控件 rules校验规则 -->
            <van-field v-model="form.username" label="账号：" placeholder="请输入账号" clearable name="username"
                :rules="usernameRules">
            </van-field>
        </van-cell-group>

        <van-cell-group>
            <van-field v-model="form.password" label="密码：" placeholder="请输入密码" clearable type="password" name="password"
                :rules="passwordRules">
            </van-field>
        </van-cell-group>

        <van-cell-group>
            <!-- repeat:重复密码验证 -->
            <van-field v-model="form.repeatPassword" label="确认密码：" placeholder="请再次输入密码" clearable type="password"
                name="confirmPassword" :rules="repeatPasswordRules">
            </van-field>
        </van-cell-group>

        <van-cell-group>
            <!-- 按钮设置 -->
            <van-button block round type="primary" native-type="submit">注册</van-button>
        </van-cell-group>
    </van-form>
    <!-- 提示 -->
    <div class="tip">注册成功后的用户可用于登录</div>
</template>

<script setup>
// 引入用户信息，token存储
import { ref, reactive } from 'vue'
import { getUser, register } from '../api';
import useToken from '../stores/token';
import useUser from '../stores/user';
import { useRouter } from 'vue-router';


const { updateToken } = useToken()
const { updateUser } = useUser()

const form = reactive({
    username: '',
    password: '',
    repeatPassword: ''
})

const ruleFormRef = ref()
const router = useRouter()

//navBar返回上级
const onClickLeft = () => history.back()

// 定义验证规则
const usernameRules = ref([
    // tirgger触发器触发onBlur事件
    { required: true, message: '用户名不能为空', tirgger: 'onBlur' },
    { pattern: /^[a-zA-Z0-9_-]{4,16}$/, message: '4到16位(字母，数字，下划线)', tirgger: 'onBlur' }
])

// password验证
const passwordRules = ref([
    { required: true, message: '密码不能为空', tirgger: 'onBlur' },
    { pattern: /^\w{6,24}$/, message: '密码必须为 6-24 位英文字母或数字', tirgger: 'onBlur' }
])

// repeatPassword验证（两种情况）
const repeatPasswordRules = ref([
    // 为空时
    { required: true, message: '密码不能为空', tirgger: 'onBlur' },
    { pattern: /^\w{6,24}$/, message: '密码必须为 6-24 位英文字母或数字', tirgger: 'onBlur' },
    // 不一致,使用validator验证器，如果密码值与表单密码值不一致，那么返回一个提示
    {
        validator: value => {
            if (value !== form.password) {
                return '两次输入密码不一致'
            }
            return true
        }
    }
])

//表单提交函数
const submitForm = async () => {
    const data = await register(form)
    debugger
    if (data) {
        updateToken(data.token)
        const user = await getUser()
        updateUser({
        isLogin: true,
        username: user.username,
        avatar: user.avatar !== '' ? config.baseURL + '/' + user.avatar : ''
    })
    //push到我的
    router.push({ name: 'user' })
    }
    //更新用户信息
   
}
const onFailed = errorInfo => {
}
</script>

<style scoped>
.Register-nav {
    --van-nav-bar-background: #ff8001;
    --van-nav-bar-title-text-color: #fff;
    --van-nav-bar-icon-color: #fff;
}

.tip {
    position: fixed;
    top: 330px;
    text-align: center;
    width: 100%;
    font-size: 14px;
    color: #666;
}
</style>